﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using CM.BL.BE;
using System.Collections.Generic;
using CM.BL.BC;

namespace WebAppBB
{
    public partial class item : System.Web.UI.Page
    {
        private RfcConfigParametersBE _objConn;
        private void checkLogin()
        {
            lblErrorMsg.InnerHtml = string.Empty;
            _objConn = (RfcConfigParametersBE)Session["objConn"];
            if (_objConn == null)
            {
                Response.Redirect("Default.aspx");
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            string retval = string.Empty; string po = string.Empty, p = string.Empty;
            this.checkLogin();
            if (!Page.IsPostBack)
            {
                po = Request.QueryString["po"];
                
                p = Request.QueryString["p"];
                if (string.IsNullOrEmpty(p))
                {
                    Response.Redirect("detail.aspx");
                }

                List<PurchaseOrderBE> lstPO = (List<PurchaseOrderBE>)Session["lstPO"];
                if (lstPO == null || lstPO.Count == 0)
                {
                    retval = PurchaseOrderBC.getInstance(Session.SessionID).getItemsForRelease(out lstPO);
                    Session["lstPO"] = lstPO;
                    lblErrorMsg.InnerHtml = retval.Trim();
                }
                PurchaseOrderBE pObj = lstPO.Find(X => X.PO == po);
                POItemBE itm = pObj.Items.Find(X => X.Pos == p);

                if (itm == null) { Response.Redirect("detail.aspx"); }

                lblPos.InnerHtml = itm.Pos;
                lblDesIt.InnerHtml = itm.Desc;
                lblCantIt.InnerHtml = itm.Cant.ToString();
                lblPrecioIt.InnerHtml = itm.Precio.ToString();
                lblMonedaIt.InnerHtml = itm.Moneda;
                if (itm.CentCO == "" && itm.ActFijo == "")
                {
                    lblActivoF.InnerHtml = " - ";
                    lblOrden.InnerHtml = " - ";
                    lblCentroCO.InnerHtml = " - ";
                    lblRespCO.InnerHtml = " - ";
                    }
                else if (itm.ActFijo == "")
                {
                    lblActivoF.InnerHtml = " - ";
                    lblOrden.InnerHtml = " - ";
                    lblCentroCO.InnerHtml = itm.CentCO;
                    lblRespCO.InnerHtml = itm.NombRespCO;
                }
                else
                {
                    lblActivoF.InnerHtml = itm.ActFijo;
                    lblCentroCO.InnerHtml = " - ";
                    lblRespCO.InnerHtml = " - ";
                    lblOrden.InnerHtml = itm.NroOrd;
                }
                lblUnitIt.InnerHtml = itm.UM;
                btnRegresar.HRef = string.Format(btnRegresar.HRef, pObj.PO);
            }
        }
    }
}